package com.fengmi.search.service.impl;

import com.fengmi.api.goods.GoodsApi;
import com.fengmi.comm.ResultVO;
import com.fengmi.comm.SearchDTO;
import com.fengmi.goods.MallGoods;
import com.fengmi.search.bean.ESGoods;
import com.fengmi.search.service.ISearchService;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ISearchServiceImpl implements ISearchService {
   @Autowired
   private ElasticsearchRestTemplate template;
   @Autowired
   private GoodsApi goodsApi;
    @Override
    public ResultVO init() {
        template.deleteIndex(ESGoods.class);//先删除索引库

        template.createIndex(ESGoods.class);
        template.putMapping(ESGoods.class);
        //索引库，通过feign调用Goods模块中获取所有商品的接口，把数据同步到es库中
        //涉及到数据量非常大
        List<MallGoods> allGoods = goodsApi.getAllGoods();//商品信息，以及品牌，一级分类，二级分类，三级分类

        List<ESGoods> esGoods = new ArrayList<>();
        allGoods.stream().forEach(mallGoods -> {
            ESGoods eg = new ESGoods();
            eg.setSpuId(mallGoods.getSpuId());
            eg.setGoodsName(mallGoods.getGoodsName());
            eg.setBrandId(mallGoods.getBrand().getId());//注意不要直接getBrandID
            eg.setBrandName(mallGoods.getBrand().getName());
            eg.setCid1id(mallGoods.getCat1().getId());
            eg.setCat1name(mallGoods.getCat1().getName());
            eg.setCid2id(mallGoods.getCat2().getId());
            eg.setCat2name(mallGoods.getCat2().getName());
            eg.setCid3id(mallGoods.getCat3().getId());
            eg.setCat3name(mallGoods.getCat3().getName());
            eg.setCreateTime(new Date());
            eg.setPrice(Double.valueOf(mallGoods.getPrice().toString()));
            eg.setImageUrl(mallGoods.getAlbumPics());
            esGoods.add(eg);
        });
        template.save(esGoods); //库中有id相同的，更新，不存在，保存
        return new ResultVO("10000","同步成功",null);
    }

    @Override
    public ResultVO searchGood(SearchDTO searchDTO) {

        PageRequest of = PageRequest.of(0, 10); //查10条
        NativeSearchQuery build=null;
        if(StringUtils.isEmpty(searchDTO.getKeyword())){ //查询所有全部商品
            MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
            build = new NativeSearchQueryBuilder().withQuery(matchAllQueryBuilder).withPageable(of).build();

        }else{   //根据商品的名字，品牌的名字，三级分类的名字 ，复合查询
            MatchQueryBuilder goodsName = QueryBuilders.matchQuery("goodsName", searchDTO.getKeyword());
            MatchQueryBuilder brandName = QueryBuilders.matchQuery("brandName", searchDTO.getKeyword());
            MatchQueryBuilder cat3name = QueryBuilders.matchQuery("cat3name", searchDTO.getKeyword());
            BoolQueryBuilder should = QueryBuilders.boolQuery().should(goodsName).should(brandName).should(cat3name);
            build = new NativeSearchQueryBuilder().withQuery(should).withPageable(of).build();
        }
        SearchHits<ESGoods> search = template.search(build, ESGoods.class);
        //解析查询结果   list<MallGoods>
        //总条数search.getTotalHits()
        List<SearchHit<ESGoods>> searchHits = search.getSearchHits();
        List<MallGoods> collect = searchHits.stream().map(hit -> {
            ESGoods eg = hit.getContent();
            MallGoods mallGoods = new MallGoods();
            mallGoods.setSpuId(eg.getSpuId());
            mallGoods.setGoodsName(eg.getGoodsName());
            mallGoods.setAlbumPics(eg.getImageUrl());
            mallGoods.setPrice(new BigDecimal(eg.getPrice()));
            return mallGoods;
        }).collect(Collectors.toList());

        return new ResultVO("1000","查询成功",collect);
    }



}
